package com.cyh.web.controller;

import com.cyh.core.framework.annotation.PassToken;
import com.cyh.core.framework.domain.dto.ResultDTO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/home")
public class HomeController {

    @PassToken
    @RequestMapping("")
    public ResultDTO home(HttpServletRequest request){

        String LOCAL_IP = "127.0.0.1";

        if (request == null) {
            return ResultDTO.Failure(403,"unknown");
        }
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Forwarded-For");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        String result="0:0:0:0:0:0:0:1".equals(ip) ? LOCAL_IP : ip;
        return ResultDTO.Success("欢迎！",result);
    }

}
